package demo.hdz.mediacodecdecode.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import demo.hdz.mediacodecdecode.R;
import demo.hdz.mediacodecdecode.widget.ViewPressEffect;

/**
 * @author hdz
 * @date 2022/06/25 11:46
 */
public class FilterAdjustView extends ConstraintLayout {
    private int type = 0;
    private TextView tvType;
    private TextView tvValue;
    private Callback callback;
    private Map<Integer, AdjustType> obj;

    public FilterAdjustView(Context context) {
        super(context);
        init(context);
    }

    public FilterAdjustView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public FilterAdjustView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);

    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    private void init(Context context) {
        ViewPressEffect effect = new ViewPressEffect();

        LayoutInflater.from(context).inflate(R.layout.layout_filter_adjust, this);

        final String[] TYPES = new String[]{"亮度", "曝光度", "对比度", "饱和度"};
        obj = new ConcurrentHashMap<>();
        obj.put(0, new AdjustType(0, "brightness", 50));
        obj.put(1, new AdjustType(1, "exposure", 50));
        obj.put(2, new AdjustType(2, "contrast", 50));
        obj.put(3, new AdjustType(3, "saturation", 50));

        tvType = findViewById(R.id.tvType);
        tvType.setText(TYPES[type]);

        effect.addView(findViewById(R.id.imgUp)).setOnClickListener(v -> {
            if (type >= TYPES.length - 1) {
                type = 0;
            } else {
                type++;
            }
            tvType.setText(TYPES[type]);
            this.initSeekBar();
        });
        effect.addView(findViewById(R.id.imgDown)).setOnClickListener(v -> {
            if (type <= 0) {
                type = TYPES.length - 1;
            } else {
                type--;
            }
            tvType.setText(TYPES[type]);
            this.initSeekBar();
        });

        this.initSeekBar();

        effect.addView(findViewById(R.id.close)).setOnClickListener(v -> {
            if (callback != null) {
                callback.onClose();
            }
        });

        effect.addView(findViewById(R.id.reset)).setOnClickListener(v -> {
            if (callback != null) {
                for (int type : obj.keySet()) {
                    obj.get(type).cur = 50;
                    callback.onValueChanged(obj.get(type).sType, 50);
                }
                tvValue.setText(String.format(Locale.getDefault(), "%d", 50));
            }
        });
    }


    private void initSeekBar() {
        final AdjustType adjustType = obj.get(type);

        tvValue = findViewById(R.id.tvValue);
        tvValue.setText(String.format(Locale.getDefault(), "%d", adjustType.cur));

        SeekBar seekBar = findViewById(R.id.seekBar);
        seekBar.setMax(100);
        seekBar.setProgress(adjustType.cur);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvValue.setText(String.format(Locale.getDefault(), "%d", progress));
                if (callback != null) {
                    obj.get(type).cur = progress;
                    callback.onValueChanged(adjustType.sType, progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }


    public interface Callback {
        void onValueChanged(String type, int value);

        void onClose();
    }

    private static class AdjustType {
        public int cur;
        public int type;
        public String sType;

        public AdjustType(int type, String sType, int cur) {
            this.cur = cur;
            this.type = type;
            this.sType = sType;
        }
    }
}
